<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class GoodsSalesOrderInfo extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];$map = [];
            $code = $this->request->post('purchase_code');
            if(!empty($code)){
                $where['i.purchase_code'] = ['like','%'.$code.'%'];
            }
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['i.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $customers_id = $this->request->post('customers_id');
            if(!empty($customers_id)){
                $where['i.customers_id'] = $customers_id;
            }
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['i.goods_code'] = $goods_code;
            }
            //$where['i.examine_status'] = '1';

            $field = empty(input('field'))?'goods_code':trim(input('field'));
            $order = empty(input('sort'))?'DESC':trim(input('sort'));

            $page = $this->request->post('page');
            $page = empty($page)?1:$page;
            $limit = $this->request->post('limit');
            $limit = empty($limit)?10:$limit;
            $app_page = config('app_page');
        $app_page = $app_page===false?false:true;
            $pageStatus = true;
            if($app_page){
                if($page<0){
                    $pageStatus = false;
                }
            }

            if($pageStatus){
                $list = Db::name('kuyou_goods_sales_order_info')->alias('i')
                    ->join('kuyou_goods_sales_order po','po.purchase_code=i.purchase_code')
                    ->join('kuyou_goods g','g.goods_code=i.goods_code')
                    ->join('kuyou_unit u','u.id=i.default_unit')
                    ->join('kuyou_customers s','s.id=i.customers_id')
                    ->join('kuyou_stores r','r.id=i.stores_id')
                    ->where($where)
                    ->page($page,$limit)
                    ->order(['i.'.$field=>$order,'i.purchase_time'=>'DESC'])
                    ->field('i.id,i.goods_code,i.purchase_code,i.purchase_type,FROM_UNIXTIME(i.purchase_time,\'%Y-%m-%d\') as purchase_time,FROM_UNIXTIME(i.delivery_time,\'%Y-%m-%d\') as delivery_time
                ,i.all_money,po.status,i.number,i.tax_included,i.money_tax_price,i.unit_price,i.number as library_number,i.money_tax_price as library_money,i.library_time,i.remark,
                g.goods_name,g.goods_specification_id,
                s.customer_name as s_name,
                r.name as r_name,
                u.name as unitname')
                    ->select();
            }else{
                $list = Db::name('kuyou_goods_sales_order_info')->alias('i')
                    ->join('kuyou_goods_sales_order po','po.purchase_code=i.purchase_code')
                    ->join('kuyou_goods g','g.goods_code=i.goods_code')
                    ->join('kuyou_unit u','u.id=i.default_unit')
                    ->join('kuyou_customers s','s.id=i.customers_id')
                    ->join('kuyou_stores r','r.id=i.stores_id')
                    ->where($where)
                    ->order(['i.'.$field=>$order,'i.purchase_time'=>'DESC'])
                    ->field('i.id,i.goods_code,i.purchase_code,i.purchase_type,FROM_UNIXTIME(i.purchase_time,\'%Y-%m-%d\') as purchase_time,FROM_UNIXTIME(i.delivery_time,\'%Y-%m-%d\') as delivery_time
                ,i.all_money,po.status,i.number,i.tax_included,i.money_tax_price,i.unit_price,i.number as library_number,i.money_tax_price as library_money,i.library_time,i.remark,
                g.goods_name,g.goods_specification_id,
                s.customer_name as s_name,
                r.name as r_name,
                u.name as unitname')
                    ->select();
            }
            $row_count = Db::name('kuyou_goods_sales_order_info')->alias('i')
                ->join('kuyou_goods_sales_order po','po.purchase_code=i.purchase_code')
                ->join('kuyou_goods g','g.goods_code=i.goods_code')
                ->join('kuyou_unit u','u.id=i.default_unit')
                ->join('kuyou_customers s','s.id=i.customers_id')
                ->join('kuyou_stores r','r.id=i.stores_id')
                ->where($where)->count();

            foreach ($list as $list_k=>$list_v){
                $purord_info = Db::name('kuyou_goods_saleord_info')->where(['order_id'=>$list_v['id'],'status'=>'1'])->field('sum(number) as number,sum(money_tax_price) as all_money')->find();
                $p_id = Db::name('kuyou_goods_saleord_info')->where(['order_id'=>$list_v['id'],'status'=>'1'])->value('purchase_id');
                $goods_excpur_info = Db::name('kuyou_goods_saleexc_info')->where(['purchase_id'=>$p_id,'status'=>'1'])->find();
                if($goods_excpur_info){
                    $purord_info['number'] = round( ($purord_info['number']-$goods_excpur_info['number']),2 );
                    $purord_info['all_money'] = round( ($purord_info['all_money']-$goods_excpur_info['money_tax_price']),2 );
                }
                if($purord_info){
                    $list[$list_k]['library_number'] = round(($list[$list_k]['library_number']-$purord_info['number']),2);
                    $list[$list_k]['library_money'] = round(($list[$list_k]['library_money']-$purord_info['all_money']),4);
                }
                if($list[$list_k]['library_number']>0 && $list[$list_k]['library_number']<$list[$list_k]['number']){
                    $list[$list_k]['status'] = 2;
                }else if($list[$list_k]['library_number']>0 && $list[$list_k]['library_number']=$list[$list_k]['number']){
                    $list[$list_k]['status'] = 0;
                }
                $purord_time = Db::name('kuyou_goods_saleord_info')->where(['order_id'=>$list_v['id'],'status'=>1])->order('times','DESC')->value('times');
                if($purord_time){
                    $list[$list_k]['library_time'] = date('Y-m-d',$purord_time);
                }
            }

            foreach ($list as $list_k=>$list_v){
                $list[$list_k]['s_type'] = 1;
                if($list_v['purchase_type']==2){
                    $list[$list_k]['number'] = -$list_v['number'];
                    $list[$list_k]['money_tax_price'] = -$list_v['money_tax_price'];
                    $list[$list_k]['library_number'] = -$list_v['library_number'];
                    $list[$list_k]['library_money'] = -$list_v['library_money'];
                }
            }

            $count = [
                'number'=>0,
                'money_tax_price'=>0,
                'library_number'=>0,
                'library_money'=>0,
            ];
            $counts = $count;
            $counts['purchase_type'] = '合计'; $counts['s_type'] = 3;
            $data = [];
            $now = isset($list[0][$field])?$list[0][$field]:'';
            foreach ($list as $list_k=>$list_v){
                if($list_v[$field]!=$now){
                    $now = $list_v[$field];
                    $row = $count;$row['purchase_type'] = '小计';$row['s_type'] = 2;
                    $data[] = $row;
                    foreach ($count as $count_k=>$count_v){
                        $count[$count_k] = $list_v[$count_k];
                        $counts[$count_k] += $list_v[$count_k];
                    }
                }else{
                    foreach ($count as $count_k=>$count_v){
                        $count[$count_k] += $list_v[$count_k];
                        $counts[$count_k] += $list_v[$count_k];
                    }
                }
                $data[] = $list_v;
                if($list_k==(sizeof($list)-1) || sizeof($list)==1){
                    $row = $count;$row['purchase_type'] = '小计';$row['s_type'] = 2;
                    $data[] = $row;
                }
            }
            $data[] = $counts;
            
            //获取设置
            $set= \think\Db::name('kuyou_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal  = $setList['price_decimal']['value'];
            foreach ($data as $data_k=>$data_v){
                $data[$data_k]['number'] = round($data_v['number'],$number_decimal);
                $data[$data_k]['library_number'] = round($data_v['library_number'],$number_decimal);
                if(isset($data_v['unit_price'])){
                    $data[$data_k]['unit_price'] = round($data_v['unit_price'],$price_decimal);
                    $data[$data_k]['tax_included'] = round($data_v['tax_included'],$price_decimal);
                    $data[$data_k]['all_money'] = round($data_v['all_money'],$price_decimal);
                }
                $data[$data_k]['library_money'] = round($data_v['library_money'],$price_decimal);
                $data[$data_k]['money_tax_price'] = round($data_v['money_tax_price'],$price_decimal);
            }
            
            $result = array("count" => $row_count, "list" => $data, "field" => $field, "order" => $order);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
